package com.hj.ct.util;

import java.io.File;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.widget.Toast;

import com.hj.ct.App;
import com.hj.ct.ActivityStackManager;
import com.hj.ct.http.RcsRequestParams;

public class AndroidUtil {
	public static String getRequestParameter() {
		StringBuffer param = new StringBuffer();
		param.append("?");
		param.append("openuuid=" + App.UDID);
		param.append("&os=android");
		param.append("&os_ver=" + android.os.Build.VERSION.RELEASE);
		param.append("&imei=" + App.IMEI);
		param.append("&imsi=" + App.IMSI);
		param.append("&term=" + android.os.Build.MODEL);
		param.append("&scwh=" + App.SCWH);
		param.append("&lang=" + App.LANG);
		param.append("&carrier=" + App.CARI);
		param.append("&net=" + App.NET);
		param.append("&lbs=");
		param.append("&ts=" + System.currentTimeMillis());
		return param.toString();
	}

	public static RcsRequestParams getBaseRequestParameter() {
		RcsRequestParams param = new RcsRequestParams();
		param.put("openuuid", App.UDID);
		param.put("os", "android");
		param.put("os_ver", android.os.Build.VERSION.RELEASE);
		param.put("imei", App.IMEI);
		param.put("imsi", App.IMSI);
		param.put("term", android.os.Build.MODEL);
		param.put("scwh", App.SCWH);
		param.put("lang", App.LANG);
		param.put("carrier", App.CARI);
		param.put("net", App.NET);
		param.put("version", getAppVersionName(App.getContext()));
		param.put("versionCode", "" + getAppVersionCode(App.getContext()));
		param.put("lbs=", "");
		param.put("ts", "" + System.currentTimeMillis());
		return param;
	}

	// 获取当前版本号
	public static String getAppVersionName(Context context) {
		String versionName = "";
		try {
			PackageManager packageManager = context.getPackageManager();
			PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
			versionName = packageInfo.versionName;
			if (TextUtils.isEmpty(versionName)) {
				return "";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return versionName;
	}

	// 获取当前版本号
	public static int getAppVersionCode(Context context) {
		int versionCode = 0;
		try {
			PackageManager packageManager = context.getPackageManager();
			PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
			versionCode = packageInfo.versionCode;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return versionCode;
	}

	public static void exits() {
		// clearNotification(GuanYingApplication.getApplictionContext());
		ActivityStackManager.getInstance().finishAll();
		android.os.Process.killProcess(android.os.Process.myPid());
	}
	
	public static int parseInt(String num){
		try{
			return Integer.parseInt(num);
		}catch(Exception e){
			return 0;
		}
	}

	public static void toast(final String str) {
		new Handler(Looper.getMainLooper()).post(new Runnable() {
			public void run() {
				Toast.makeText(App.getContext(), str, Toast.LENGTH_LONG).show();
			}
		});
	}

	// 扫描指定文件
	public static void scanFileAsync(final Context ctx, final String filePath) {
		new Handler(Looper.getMainLooper()).post(new Runnable() {
			public void run() {
				Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
				scanIntent.setData(Uri.fromFile(new File(filePath)));
				ctx.sendBroadcast(scanIntent);
			}
		});
	}

	// 扫描指定目录
	public static final String ACTION_MEDIA_SCANNER_SCAN_DIR = "android.intent.action.MEDIA_SCANNER_SCAN_DIR";

	public static void scanDirAsync(final Context ctx, final String dir) {
		new Handler(Looper.getMainLooper()).post(new Runnable() {
			public void run() {
				Intent scanIntent = new Intent(ACTION_MEDIA_SCANNER_SCAN_DIR);
				scanIntent.setData(Uri.fromFile(new File(dir)));
				ctx.sendBroadcast(scanIntent);
			}
		});
	}

	public static boolean checkApkExist(Context context, String packageName) {
		if (packageName == null || "".equals(packageName))
			return false;
		try {
			ApplicationInfo info = context.getPackageManager().getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
			return true;
		} catch (Exception e) {
			return false;
		}

	}
}
